@Library('jenkins-shared-lib') _ // 引用共享库

pipeline {
    agent any
        // 设置全局选项
    options {
        // 构建超时时间设置为 1 小时
        timeout(time: 1, unit: 'HOURS')
        // 保持最近 10 次构建记录
        buildDiscarder(logRotator(numToKeepStr: '10'))
        // 禁用并发构建
        disableConcurrentBuilds()
    }
    // 修改后点击"Build with Parameters",不会更新最新的参数修改，修改后第一次运行会出现问题
    parameters {
        string(name: 'PERSON', defaultValue: 'World', description: 'Who should I say hello to?')
        choice(name: 'ENVIRONMENT', choices: ['dev', 'test', 'prod'], description: 'Select deployment environment')
        booleanParam(name: 'DEPLOY', defaultValue: false, description: 'Deploy after build?')
        string(name: 'NEW_PARAM', defaultValue: 'new', description: 'New parameter.')
    }

    stages {
        stage('参数检查') {
            steps {
                script {
                    // 调用检查函数，如果参数变更则终止构建
                    checkParams()
                }
            }
        }
        stage('Example') {
            steps {
                echo "Hello ${params.PERSON}!"
                echo "Selected environment: ${params.ENVIRONMENT}"
                echo "Deploy after build? ${params.DEPLOY}"
                echo "NEW_PARAM: ${params.NEW_PARAM}"
                // ✅ 将 if 逻辑放在 script 块中
                script {
                    if (params.DEPLOY) {
                        echo "Deploying to ${params.ENVIRONMENT}..."
                        // 在这里添加你的部署步骤，例如：
                        // sh 'kubectl apply -f deployment.yaml'
                    } else {
                        echo "Skipping deployment."
                    }
                }
            }
        }
    }
}